package com.zhisuo.apicenter.util;

import com.alibaba.fastjson.JSONException;
import com.github.qcloudsms.SmsSingleSender;
import com.github.qcloudsms.SmsSingleSenderResult;
import org.apache.commons.logging.LogFactory;

import javax.xml.ws.http.HTTPException;
import java.io.IOException;

/**
 * Tencent Cloud Sms Sendsms
 * https://cloud.tencent.com/document/product/382/38778
 */
public class SendSmsUtil {
    public static final Integer APPID=1400406900;
    public static final String APPKEY="fe0ba4f6ae72a477e63ebe847a789a98";
    //短信模板ID，需要在短信应用中申请
    public static final Integer templateId=737501;
    // 签名
    public static final String smsSign = "智梭无人车";





    public static String sendSmsPhoneCode( String[] params,  String[] phoneNumbers ) throws Exception{
        SmsSingleSenderResult result =new SmsSingleSenderResult();

        try {

            //验证码 params

            SmsSingleSender ssender = new SmsSingleSender(APPID, APPKEY);
            result = ssender.sendWithParam("86", phoneNumbers[0],
                    templateId, params, smsSign, "", "");  // 签名不能为空串
            LogFactory.getLog(new SendSmsUtil().getClass()).info("sendSmsPhoneCode-----------------------:" +phoneNumbers[0] +result);
        } catch (HTTPException e) {
            // HTTP响应码错误
            e.printStackTrace();
        } catch (JSONException e) {
            // json解析错误
            e.printStackTrace();
        } catch (IOException e) {
            // 网络IO错误
            e.printStackTrace();
        }
        return result.toString();
    }


    /**
     * 发送取商品短信
     * @param phoneNumbers
     * @return
     * @throws Exception
     */
    public static String sendSmsGetSellerBox(   String[] params,String[] phoneNumbers ) throws Exception{
        SmsSingleSenderResult result =new SmsSingleSenderResult();

        try {
            //验证码 params
            SmsSingleSender ssender = new SmsSingleSender(APPID, APPKEY);
            result = ssender.sendWithParam("86", phoneNumbers[0],
                    754686, params, smsSign, "", "");  // 签名不能为空串
            LogFactory.getLog(new SendSmsUtil().getClass()).info("sendSmsGetSellerBox-----------------------:" +phoneNumbers[0] +result);
        } catch (Exception e) {
            // HTTP响应码错误
            e.printStackTrace();
        }

        return result.toString();

    }

    /**
     * 快递员存入快件
     * @param phoneNumbers
     * @return
     * @throws Exception
     */
    public static String sendSmsPutBox(  String[] phoneNumbers ) throws Exception{
        SmsSingleSenderResult result =new SmsSingleSenderResult();

        try {

            //验证码 params

            SmsSingleSender ssender = new SmsSingleSender(APPID, APPKEY);
            result = ssender.sendWithParam("86", phoneNumbers[0],
                    742462, new String[]{}, smsSign, "", "");  // 签名不能为空串
            LogFactory.getLog(new SendSmsUtil().getClass()).info("sendSmsPutBox-----------------------:" +phoneNumbers[0] +result);
        } catch (HTTPException e) {
            // HTTP响应码错误
            e.printStackTrace();
        } catch (JSONException e) {
            // json解析错误
            e.printStackTrace();
        } catch (IOException e) {
            // 网络IO错误
            e.printStackTrace();
        }

        return result.toString();

    }



    /**
     * 发送取件短信
     * @param phoneNumbers
     * @return
     * @throws Exception
     */
    public static String sendSmsGetBox(   String[] params,String[] phoneNumbers ) throws Exception{
//        SmsSingleSenderResult result =new SmsSingleSenderResult();
//
//        try {
//
//            //验证码 params
//
//            SmsSingleSender ssender = new SmsSingleSender(APPID, APPKEY);
//            result = ssender.sendWithParam("86", phoneNumbers[0],
//                    745821, params, smsSign, "", "");  // 签名不能为空串
//            LogFactory.getLog(new SendSmsUtil().getClass()).info("sendSmsGetBox-----------------------:" +phoneNumbers[0] +result);
//        } catch (HTTPException e) {
//            // HTTP响应码错误
//            e.printStackTrace();
//        } catch (JSONException e) {
//            // json解析错误
//            e.printStackTrace();
//        } catch (IOException e) {
//            // 网络IO错误
//            e.printStackTrace();
//        }
//
//        return result.toString();
        return "";

    }


    /**
     * 发送超时短信
     * @param phoneNumbers
     * @return
     * @throws Exception
     */
    public static String sendSmsAgainGetBox(   String[] params,String[] phoneNumbers ) throws Exception{
        SmsSingleSenderResult result =new SmsSingleSenderResult();

        try {

            //验证码 params

            SmsSingleSender ssender = new SmsSingleSender(APPID, APPKEY);
            result = ssender.sendWithParam("86", phoneNumbers[0],
                    754757, new String[]{}, smsSign, "", "");  // 签名不能为空串
            LogFactory.getLog(new SendSmsUtil().getClass()).info("sendSmsAgainGetBox-----------------------:" +phoneNumbers[0] +result);
        } catch (HTTPException e) {
            // HTTP响应码错误
            e.printStackTrace();
        } catch (JSONException e) {
            // json解析错误
            e.printStackTrace();
        } catch (IOException e) {
            // 网络IO错误
            e.printStackTrace();
        }

        return result.toString();

    }


    public static void main(String[] args) throws Exception{
        try {

            //验证码
            String[] params = {"1234"};
            String[] phoneNumbers = {"18602582663"};
            String result=SendSmsUtil.sendSmsGetSellerBox(params,phoneNumbers);// 签名不能为空串
            System.out.println(result);
        } catch (HTTPException e) {
            // HTTP响应码错误
            e.printStackTrace();
        } catch (JSONException e) {
            // json解析错误
            e.printStackTrace();
        } catch (IOException e) {
            // 网络IO错误
            e.printStackTrace();
        }
    }
}